    <?php

class Institute extends CI_Model {
    
    
    function __construct()
{
    parent::__construct();
    $this->load->database();
}
    
    

    private $table = 'institutes'; 
    function getConsultasInstituto($id_curso,$contar= false,$usuario = NULL ){
     
        $arr = array();
        if($id_curso != null){
         
        $arr["curso"] = $id_curso;  
         
        }
     
        
        
    $row = $this->db->select('*')->get_where("contactos",$arr);
    if($contar){ 
       
       return $row->num_rows();;       
   }else{
       print_r($row->result());
       return $row->result();;   
   }
     
            return $row;
    }
    function getInstitutoUser($id_user){
        
        
    $query = $this->db->query("select institutes.* from usuarios_institutos inner JOIN institutes on institutes.id = usuarios_institutos.instituto_id WHERE user_id = " .  $id_user);
       
     return $query->result();
    }
    public function getAllRecords() {
        $rows = $this->db->get($this->table)->result();
        return $rows;
    }
    public function getCursosInstituto($id){
            $query = "select cursos.* from cursos INNER JOIN institutes on institutes.id = cursos.id_instituto where institutes.id =$id";
             $row = $this->db->query($query)->result();
             return $row;  
        }
    public function getRecordById($id) {
    
            
        $row = $this->db->query("select  * from $this->table where id=$id")->result();    
        return $row;
    }
    public function getRecordBySlug($slug) {
      

            $row = $this->db->select('*')->get_where($this->table, array(
                    'slug' => $slug
                ))->result();

     
            return $row;
    }
    public function guardarContacto($data){
            try {
               $this->db->insert('contactos', $data); 
              return $this->db;   
            } catch (Exception $exc) {
                echo $exc->getTraceAsString();
            }

          

        }
    function getNumInstitutos($id_ciudad,$categorias)
    {

        $query ="SELECT * FROM institutes
     INNER JOIN cursos on cursos.id_instituto = institutes.id
     INNER JOIN categories on categories.id = cursos.categoria 
    where categories.slug = '$categorias' and institutes.city_id =$id_ciudad and institutes.publicado = 1 GROUP BY institutes.id ";
                $row = $this->db->query($query)->result();

        return count($row);
    }
    function getTodosInstitutos($limit,$start,$city,$categoriaSlug,$activo=1)
{
    if($categoriaSlug!=''){
        $filtro=array('city_id'=>$city);
    }else{
        $filtro=array(
                        'city_id'=>$city,
                        'slug'=>$categoriaSlug
                    );
        
    }
    $filtro["activo"] = $activo;
    
    $this->db->limit($limit,$start);

   $query =  $this->db->query("SELECT institutes.* from institutes INNER JOIN cursos on cursos.id_instituto = institutes.id inner JOIN categories on categories.id = cursos.categoria WHERE categories.slug ='$categoriaSlug' and institutes.publicado = $activo  GROUP BY institutes.id LIMIT $start,$limit ");

   return $query->result();      // $row = $this->db->select('*')->get_where($this->table,$filtro
                    
               

}
    }

?>
